import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public User login(String username, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, MD5Util.encrypt(password)); // 加密比对
            rs = pstmt.executeQuery();

            if (rs.next()) {
                String userId = rs.getString("user_id");
                String userTypeStr = rs.getString("user_type");
                String phone = rs.getString("phone");
                UserType userType = UserType.valueOf(userTypeStr);

                // 根据用户类型创建实例
                switch (userType) {
                    case ADMIN:
                        return new Admin(userId, username, password, phone);
                    case MANAGER:
                        return new Manager(userId, username, password, phone);
                    case RECEPTIONIST:
                        return new Receptionist(userId, username, password, phone);
                    default:
                        return null;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return null;
    }

    @Override
    public boolean addUser(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO user (user_id, username, password, user_type, phone, register_time) " +
                         "VALUES (?, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getUserId());
            pstmt.setString(2, user.getUsername());
            pstmt.setString(3, user.getPassword()); // 已加密
            pstmt.setString(4, user.getUserType().name());
            pstmt.setString(5, user.getPhone());
            pstmt.setString(6, user.getRegisterTime().toString());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeResources(conn, pstmt, null);
        }
    }

    @Override
    public List<User> getAllUsers() {
        List<User> users = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM user";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                String userId = rs.getString("user_id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String userTypeStr = rs.getString("user_type");
                String phone = rs.getString("phone");
                UserType userType = UserType.valueOf(userTypeStr);

                User user = null;
                switch (userType) {
                    case ADMIN:
                        user = new Admin(userId, username, password, phone);
                        break;
                    case MANAGER:
                        user = new Manager(userId, username, password, phone);
                        break;
                    case RECEPTIONIST:
                        user = new Receptionist(userId, username, password, phone);
                        break;
                }
                if (user != null) users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return users;
    }

    @Override
    public boolean updatePassword(String username, String newPassword) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE user SET password = ? WHERE username = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, MD5Util.encrypt(newPassword));
            pstmt.setString(2, username);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeResources(conn, pstmt, null);
        }
    }
}
    